package com.augurit.wf.bi.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.wf.bi.convert.BusTemplateNodeConvertor;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfBusTemplateTypeService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.bi.web.form.WfBusTemplateTypeForm;
import com.augurit.wf.common.extmodel.BusTemplateNode;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "wf-bus-template.action", type = "redirect")
})
public class BusTemplateTreeAction extends ExtCrudActionSupport<BusTemplateNode>{


	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private IWfBusTemplateTypeService wfBusTemplateTypeService;
	
	
	private BusTemplateNode treeNode;
	
	/**
	 * 异步展开流程业务模板树
	 */
	public String expandTemplateTree()throws Exception {
		List<BusTemplateNode> list = null;
		
		if(treeNode.getEntityId() != null){
			//业务模板树上的根结点展开
			if(treeNode.getEntityId().toString().equals(BusTemplateNode.ROOT_ID)){
				List<WfBusTemplateTypeForm> typeList = wfBusTemplateTypeService.getAll();
				list = BusTemplateNodeConvertor.convertCodesToList(typeList);
			}
			//业务模板树上的类型结点展开
			else{
				List<WfBusTemplateForm> templateList = wfBusTemplateService.getByTemplateTypeId(treeNode.getEntityId());
				list = BusTemplateNodeConvertor.convertTemplatesToList(templateList);
			}
		}
		
		ExtBaseAction.extRenderListJson(list, false);
		return null;
	}
	
	/**
	 * 新增模板类型
	 */
	public String addTemplateType() throws Exception {
		String typeName = treeNode.getText();
		
		if(typeName != null && typeName.trim().length() > 0){
			WfBusTemplateTypeForm form = new WfBusTemplateTypeForm();
			form.setTemplateTypeName(typeName);
			wfBusTemplateTypeService.save(form);
			ExtBaseAction.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 编辑模板类型
	 */
	public String editTemplateType() throws Exception {
		String typeName = treeNode.getText();
		
		if(treeNode.getEntityId() != null && typeName != null && typeName.trim().length() > 0){
			
			WfBusTemplateTypeForm form = new WfBusTemplateTypeForm();
			form.setTemplateTypeName(typeName);
			wfBusTemplateTypeService.changeTypeName(treeNode.getEntityId(), typeName);
			ExtBaseAction.extRenderSuccess();
		}
		return null;
	}
	
	/**
	 * 删除模板类型
	 */
	public String deleteTemplateType() throws Exception {
		if(treeNode.getEntityId() != null)
			wfBusTemplateTypeService.delete(treeNode.getEntityId());
		
		return null;
	}
	
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		treeNode = new BusTemplateNode();
	}
	

	public BusTemplateNode getModel() {
		return treeNode;
	}
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	// 属性getter和setter方法
	public BusTemplateNode getTreeNode() {
		return treeNode;
	}

	public void setTreeNode(BusTemplateNode treeNode) {
		this.treeNode = treeNode;
	}
}
